//
//  TaskGuiJiStatusHeadView.m
//  LogisticsSupplier
//
//  Created by 吕晶 on 2021/4/8.
//  Copyright © 2021 吕晶. All rights reserved.
//

#import "TaskGuiJiStatusHeadView.h"

@interface TaskGuiJiStatusHeadView () <UIScrollViewDelegate>

@property (nonatomic, strong) UIButton *locationBtn;//司机定位
@property (nonatomic, strong) UIButton *sheBeiBtn;//设备定位
@property (nonatomic, strong) UIButton *boxBtn;//箱子定位
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;

@end

@implementation TaskGuiJiStatusHeadView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        
        self.locationBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 107, 30)];
        [self.locationBtn setTitle:@"司机定位" forState:0];
        [self.locationBtn setTitleColor:HexColor(0xffffff) forState:0];
        self.locationBtn.titleLabel.font = BoldFontOfSize(14);
        self.locationBtn.backgroundColor = Color_ThemeColor;
        self.locationBtn.layer.masksToBounds = YES;
        self.locationBtn.layer.cornerRadius = 15;
        [self.locationBtn setImage:[UIImage imageNamed:@"icon_shishi_location"] forState:0];
        [self.locationBtn addTarget:self action:@selector(locationClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.locationBtn];
        
        self.sheBeiBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.locationBtn.nim_right + 15, 0, 107, 30)];
        [self.sheBeiBtn setTitle:@"设备定位" forState:0];
        [self.sheBeiBtn setTitleColor:HexColor(0xffffff) forState:0];
        self.sheBeiBtn.titleLabel.font = BoldFontOfSize(14);
        self.sheBeiBtn.backgroundColor = Color_ThemeColor;
        self.sheBeiBtn.layer.masksToBounds = YES;
        self.sheBeiBtn.layer.cornerRadius = 15;
        [self.sheBeiBtn setImage:[UIImage imageNamed:@"icon_shishi_location"] forState:0];
        [self.sheBeiBtn addTarget:self action:@selector(sheBeiClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.sheBeiBtn];
        
        self.boxBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.sheBeiBtn.nim_right + 15, 0, 107, 30)];
        [self.boxBtn setTitle:@"箱子定位" forState:0];
        [self.boxBtn setTitleColor:HexColor(0xffffff) forState:0];
        self.boxBtn.titleLabel.font = BoldFontOfSize(14);
        self.boxBtn.backgroundColor = [UIColor darkGrayColor];
        self.boxBtn.layer.masksToBounds = YES;
        self.boxBtn.layer.cornerRadius = 15;
        [self.boxBtn setImage:[UIImage imageNamed:@"icon_shishi_location"] forState:0];
        [self.boxBtn addTarget:self action:@selector(boxClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.boxBtn];
        self.boxBtn.userInteractionEnabled = NO;
        
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.locationBtn.nim_bottom + 10, ScreenW - 30, 220)];
        self.scrollView.backgroundColor = [UIColor clearColor];
        self.scrollView.delegate = self;
        self.scrollView.showsHorizontalScrollIndicator = false;
        self.scrollView.bounces = NO;
        [self addSubview:self.scrollView];
        
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((frame.size.width - 200) * 0.5, self.scrollView.nim_bottom - 25, 200, 25)];
        self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
        self.pageControl.currentPageIndicatorTintColor = Color_ThemeColor;
        [self addSubview:self.pageControl];
    }
    return self;
}

- (void)sheBeiClick {
    if (self.sheBeiLocationBlock) {
        self.sheBeiLocationBlock();
    }
}

- (void)boxClick {
    
}

- (void)locationClick {
    if (self.shiShiLocationBlock) {
        self.shiShiLocationBlock();
    }
}

-(void)setModel:(TaskGuiJiModel *)model {
    _model = model;
    [self configUI];
}

- (void)configUI {
    CGFloat viewW = 0;
//    if (self.model.list.count > 1) {
//        self.scrollView.nim_width = ScreenW - 15;
//        viewW = ScreenW - 35 - 15;
//    }else {
//        self.scrollView.nim_width = ScreenW - 30;
        viewW = ScreenW - 30;
//    }
    self.scrollView.contentSize = CGSizeMake(viewW * self.model.list.count, self.scrollView.nim_height);
    self.scrollView.pagingEnabled = YES;
    self.pageControl.numberOfPages = self.model.list.count;
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.model.list enumerateObjectsUsingBlock:^(TaskGuiJiOrderModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        TaskGuiJiStatusHeadOrderView *view = [[TaskGuiJiStatusHeadOrderView alloc] initWithFrame:CGRectMake(viewW * idx, 0, viewW, self.scrollView.nim_height)];
        view.model = obj;
        [self.scrollView addSubview:view];
    }];
    if (self.model.list.firstObject.equipment_id && self.model.list.firstObject.equipment_starttime) {
        self.sheBeiBtn.backgroundColor = Color_ThemeColor;
    }else {
        self.sheBeiBtn.backgroundColor = [UIColor darkGrayColor];
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.pageControl.currentPage = self.scrollView.contentOffset.x / self.scrollView.nim_width;
    TaskGuiJiOrderModel *model = self.model.list[self.pageControl.currentPage];
    if (model.equipment_id && model.equipment_starttime) {
        self.sheBeiBtn.backgroundColor = Color_ThemeColor;
    }else {
        self.sheBeiBtn.backgroundColor = [UIColor darkGrayColor];
    }
    if (self.scrollBlock) {
        self.scrollBlock(self.pageControl.currentPage);
    }
}

@end

@interface TaskGuiJiStatusHeadOrderView ()

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UIView *addressBGView;
@property (nonatomic, strong) UILabel *fromAddLab;
@property (nonatomic, strong) UIImageView *statusImgView;
@property (nonatomic, strong) UILabel *toAddLab;

@property (nonatomic, strong) UIImageView *faImgView;
@property (nonatomic, strong) UILabel *faAddressLab;
@property (nonatomic, strong) UILabel *marginLine;
@property (nonatomic, strong) UIImageView *shouImgView;
@property (nonatomic, strong) UILabel *shAddressLab;
@property (nonatomic, strong) UILabel *linkLine;

@property (nonatomic, strong) UILabel *goodPieceTitleLab;
@property (nonatomic, strong) UILabel *goodPieceLab;
@property (nonatomic, strong) UILabel *weightTitleLab;
@property (nonatomic, strong) UILabel *weightLab;
@property (nonatomic, strong) UILabel *volumeTitleLab;
@property (nonatomic, strong) UILabel *volumeLab;

@end

@implementation TaskGuiJiStatusHeadOrderView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        self.bgView.backgroundColor = HexColor(0xffffff);
        [self addSubview:self.bgView];
        
        self.addressBGView = [UIView new];
        self.addressBGView.backgroundColor = HexColor(0xF7F7FA);
        self.addressBGView.layer.cornerRadius = 2;
        [self.bgView addSubview:self.addressBGView];
        
        self.fromAddLab = [UILabel new];
        self.fromAddLab.textColor = HexColor(0x2B2C44);
        self.fromAddLab.font = BoldFontOfSize(20);
        self.fromAddLab.textAlignment = NSTextAlignmentCenter;
        [self.addressBGView addSubview:self.fromAddLab];
        
        self.statusImgView = [UIImageView new];
        [self.addressBGView addSubview:self.statusImgView];
        
        self.toAddLab = [UILabel new];
        self.toAddLab.textColor = HexColor(0x2B2C44);
        self.toAddLab.font = BoldFontOfSize(20);
        self.toAddLab.textAlignment = NSTextAlignmentCenter;
        [self.addressBGView addSubview:self.toAddLab];

        self.faImgView = [UIImageView new];
        self.faImgView.image = [UIImage imageNamed:@"icon_add_ti"];
        [self.bgView addSubview:self.faImgView];
        
        self.faAddressLab = [UILabel new];
        self.faAddressLab.textColor = HexColor(0x2B2C44);
        self.faAddressLab.font = BoldFontOfSize(14);
        [self.bgView addSubview:self.faAddressLab];
        
        self.shouImgView = [UIImageView new];
        self.shouImgView.image = [UIImage imageNamed:@"icon_add_song"];
        [self.bgView addSubview:self.shouImgView];
        
        self.shAddressLab = [UILabel new];
        self.shAddressLab.textColor = HexColor(0x2B2C44);
        self.shAddressLab.font = BoldFontOfSize(14);
        [self.bgView addSubview:self.shAddressLab];
        
        self.marginLine = [UILabel new];
        self.marginLine.backgroundColor = Color_backgroundColor;
        [self.bgView addSubview:self.marginLine];
        
        self.linkLine = [UILabel new];
        self.linkLine.backgroundColor = HexColorA(0x3D68EB, 0.2);
        [self.bgView addSubview:self.linkLine];
        
        self.goodPieceTitleLab = [UILabel new];
        self.goodPieceTitleLab.textColor = HexColor(0x9C9DB3);
        self.goodPieceTitleLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.goodPieceTitleLab];
        
        self.goodPieceLab = [UILabel new];
        self.goodPieceLab.textColor = HexColor(0x2B2C44);
        self.goodPieceLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.goodPieceLab];
        
        self.weightTitleLab = [UILabel new];
        self.weightTitleLab.textColor = HexColor(0x9C9DB3);
        self.weightTitleLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.weightTitleLab];
        
        self.weightLab = [UILabel new];
        self.weightLab.textColor = HexColor(0x2B2C44);
        self.weightLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.weightLab];
        
        self.volumeTitleLab = [UILabel new];
        self.volumeTitleLab.textColor = HexColor(0x9C9DB3);
        self.volumeTitleLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.volumeTitleLab];
        
        self.volumeLab = [UILabel new];
        self.volumeLab.textColor = HexColor(0x2B2C44);
        self.volumeLab.font = [UIFont systemFontOfSize:12];
        [self.bgView addSubview:self.volumeLab];
    }
    return self;
}

-(void)setModel:(TaskGuiJiOrderModel *)model {
    _model = model;
    [self configUI];
}

- (void)configUI {
    self.addressBGView.frame = CGRectMake(10, 10, self.bgView.nim_width - 20, 68);
    
    self.statusImgView.nim_width = 59;
    self.statusImgView.nim_height = 10;
    self.statusImgView.nim_left = (self.bgView.nim_width - self.statusImgView.nim_width) * 0.5;
    self.statusImgView.nim_centerY = self.addressBGView.nim_height * 0.5;
    [self.statusImgView setImage:[UIImage imageNamed:@"operationbill_status"]];

    self.fromAddLab.nim_left = 10;
    self.fromAddLab.text = self.model.s_city;
    [self.fromAddLab sizeToFit];
    self.fromAddLab.nim_width = self.statusImgView.nim_left - 20;
    self.fromAddLab.nim_centerY = self.statusImgView.nim_centerY;

    self.toAddLab.nim_left = 10 + self.statusImgView.nim_right;
    self.toAddLab.text = self.model.r_city;
    [self.toAddLab sizeToFit];
    self.toAddLab.nim_width = self.statusImgView.nim_left - 20;
    self.toAddLab.nim_centerY = self.statusImgView.nim_centerY;

    self.faAddressLab.text = [NSString stringWithFormat:@"%@%@%@",self.model.s_province,self.model.s_city,self.model.s_town];
    [self.faAddressLab sizeToFit];
    self.faAddressLab.nim_left = 59;
    self.faAddressLab.nim_top = 15 + self.addressBGView.nim_bottom;
    self.faAddressLab.nim_width = self.bgView.nim_width - 10 - self.faAddressLab.nim_left;


    self.marginLine.nim_left = self.faAddressLab.nim_left - 5;
    self.marginLine.nim_top = self.faAddressLab.nim_bottom + 15;
    self.marginLine.nim_width = self.bgView.nim_width - self.marginLine.nim_left;
    self.marginLine.nim_height = 1;

    self.shAddressLab.text = [NSString stringWithFormat:@"%@%@%@",self.model.r_province,self.model.r_city,self.model.r_town];
    [self.shAddressLab sizeToFit];
    self.shAddressLab.nim_left = 59;
    self.shAddressLab.nim_top = 15 + self.marginLine.nim_bottom;
    self.shAddressLab.nim_width = self.bgView.nim_width - 10 - self.shAddressLab.nim_left;

    self.faImgView.nim_width = 22;
    self.faImgView.nim_height = 22;
    self.faImgView.nim_left = 20;
    self.faImgView.nim_centerY = self.faAddressLab.nim_centerY;

    self.shouImgView.nim_width = 22;
    self.shouImgView.nim_height = 22;
    self.shouImgView.nim_left = 20;
    self.shouImgView.nim_centerY = self.shAddressLab.nim_centerY;

    self.linkLine.nim_width = 2;
    self.linkLine.nim_top = self.faImgView.nim_bottom + 3;
    self.linkLine.nim_height = self.shouImgView.nim_top - 3 - self.linkLine.nim_top;
    self.linkLine.nim_centerX = self.faImgView.nim_centerX;

    CGFloat titleW = (self.bgView.nim_width - 40) * 0.33333;

    self.goodPieceTitleLab.text = @"件数：";
    [self.goodPieceTitleLab sizeToFit];
    self.goodPieceTitleLab.nim_left = 20;
    self.goodPieceTitleLab.nim_top = self.shouImgView.nim_bottom + 15;

    self.goodPieceLab.text = [NSString stringWithFormat:@"%@件",self.model.goodsJnum];
    [self.goodPieceLab sizeToFit];
    self.goodPieceLab.nim_left = self.goodPieceTitleLab.nim_right;
    self.goodPieceLab.nim_centerY = self.goodPieceTitleLab.nim_centerY;
    self.goodPieceLab.nim_width = 20 + titleW - self.goodPieceLab.nim_left;

    self.weightTitleLab.text = @"重量：";
    [self.weightTitleLab sizeToFit];
    self.weightTitleLab.nim_left = self.goodPieceLab.nim_right;
    self.weightTitleLab.nim_centerY = self.goodPieceTitleLab.nim_centerY;

    self.weightLab.text = [self.model.allWeight floatValue] ? [NSString stringWithFormat:@"%@KG",self.model.allWeight] : @"--KG";
    [self.weightLab sizeToFit];
    self.weightLab.nim_left = self.weightTitleLab.nim_right;
    self.weightLab.nim_centerY = self.weightTitleLab.nim_centerY;
    self.weightLab.nim_width = self.goodPieceLab.nim_right + titleW - self.weightLab.nim_left;

    self.volumeTitleLab.text = @"货物体积：";
    [self.volumeTitleLab sizeToFit];
    self.volumeTitleLab.nim_left = self.weightLab.nim_right;
    self.volumeTitleLab.nim_centerY = self.weightLab.nim_centerY;

    self.volumeLab.text = [self.model.allVolume floatValue] ? [NSString stringWithFormat:@"%@m³",self.model.allVolume] : @"--m³";
    [self.volumeLab sizeToFit];
    self.volumeLab.nim_left = self.volumeTitleLab.nim_right;
    self.volumeLab.nim_centerY = self.volumeTitleLab.nim_centerY;
    self.volumeLab.nim_width = self.weightLab.nim_right + titleW - self.volumeLab.nim_left;
}

@end
